package cn.lix.sc.rpc.server.handler;

import cn.lix.sc.rpc.impl.common.constant.SystemCnt;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

public class RequestHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getHeader(SystemCnt.REQ_ID) == null){
            MDC.put(SystemCnt.REQ_ID, UUID.randomUUID().toString());
        }else{
            MDC.put(SystemCnt.REQ_ID, request.getHeader(SystemCnt.REQ_ID));
        }
        return true;
    }
}
